package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

import sessionBeans.AccessRemote;

/**
 * Servlet implementation class Messages
 */
public class MessagesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessagesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		
		try{
			
						
//			Hashtable<String,String> env = new Hashtable<String,String>();
//			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
//			env.put(Context.PROVIDER_URL,"localhost:8080");
			Context ctx = new InitialContext();
			
			AccessRemote accessRemote = (AccessRemote)ctx.lookup("AccessRemoteJNDI");
			//AccessBean accessRemote= (AccessBean)ctx.lookup("AccessRemote");
	
//			UserObject user=new UserObject();
//			user.set.
			
			Collection id = accessRemote.getMessages( );
			
			out.print(id);
			
			if(id != null)
				out.print("<h1>get meaages successfully : " + id + "</h1>");			
			else			
				out.print("<h1>get meaages failed </h1>");			


		}
		catch (Exception e) {
			e.printStackTrace();
			out = response.getWriter();
			out.print("<h1>there is Exception !</h1>");
		}
		
		out.print("<br><br><a href=\"/swimtwoWEB/Main.jsp\"></a><br><br>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
